using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;

namespace Granite.Wcf.PocoService
{
	class DefaultInstanceProvider : IInstanceProvider
	{
		// This provider implements a "PerCall" instance mode - a new instance of the service class is used
		// for each incoming message. Changing the provider (to implement a singleton pattern, for example)
		// is simple. More information later on the post about IInstanceProvider.
		readonly Type serviceType;

		public DefaultInstanceProvider(Type serviceType)
		{
			this.serviceType = serviceType;
		}
		/// <summary>
		/// Returns a service object given the specified <see cref="T:System.ServiceModel.InstanceContext"></see> object.
		/// </summary>
		/// <returns>
		/// A user-defined service object.
		/// </returns>
		/// <param name="instanceContext">
		/// The current <see cref="T:System.ServiceModel.InstanceContext"></see> object.
		/// </param>
		public object GetInstance(InstanceContext instanceContext)
		{
			return Activator.CreateInstance(serviceType);
		}
		/// <summary>
		/// Returns a service object given the specified <see cref="T:System.ServiceModel.InstanceContext"></see> object.
		/// </summary>
		/// <returns>
		/// The service object.
		/// </returns>
		/// <param name="instanceContext">
		/// The current <see cref="T:System.ServiceModel.InstanceContext"></see> object.
		/// </param>
		/// <param name="message">
		/// The message that triggered the creation of a service object.
		/// </param>
		public object GetInstance(InstanceContext instanceContext, Message message)
		{
			return Activator.CreateInstance(serviceType);
		}
		/// <summary>
		/// Called when an <see cref="T:System.ServiceModel.InstanceContext"></see> object recycles a service object.
		/// </summary>
		/// <param name="instanceContext">
		/// The service's instance context.
		/// </param>
		/// <param name="instance">
		/// The service object to be recycled.
		/// </param>
		public void ReleaseInstance(InstanceContext instanceContext, object instance)
		{
		}
	}
}
